//
//  CommentController.m
//  吃货·上海
//
//  Created by 濮亚军 on 15/11/30.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "CommentController.h"
#import "CommentView.h"
#import <BmobSDK/Bmob.h>
@interface CommentController ()

//发表评论
@property(nonatomic, strong)CommentView *commentView;

@end

@implementation CommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = KMyColor;
    self.commentView = [[CommentView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:_commentView];
    
    //评论手势
    UITapGestureRecognizer *submitTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(submitTapAction:)];
    [self.commentView.submitImageView addGestureRecognizer:submitTap];
}

//手势方法
-(void)submitTapAction:(UITapGestureRecognizer *)submitTap
{
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (!bUser)
    {
        NSLog(@"成功");
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"请先登录"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alterView show];
    }
    else
    {
        if (self.commentView.commentTV.text.length == 0)
        {
            UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"内容不能为空"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alterView show];
        }
        else
        {
            BmobObject *userComment = [BmobObject objectWithClassName:[NSString stringWithFormat:@"Comment%@%@",self.userShareModel.userName, self.userShareModel.objectId]];
            [userComment setObject:self.commentView.commentTV.text forKey:@"comment"];
            [userComment setObject:bUser.username forKey:@"username"];
            BmobFile *iconFile = [bUser objectForKey:@"icon"];
            [userComment setObject:iconFile forKey:@"userIcon"];
            [userComment saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                //进行操作
                if (isSuccessful)
                {
                    NSLog(@"成功");
                    UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"评论成功"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    [alterView show];
                    [self.navigationController popViewControllerAnimated:NO];
                }
                else
                {
                    NSLog(@"失败");
                    NSLog(@"%@",error);
                }
            }];
        }
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
